// pages/myTeam/myTeam.js
const app = getApp();
var utils = require('../../utils/util.js');
Page({

  /**
  * 页面的初始数据
  */
  data: {
    session_key:'',
    currentTab: 0,
    createTeamstatus: 0,
    createteamlist: [],
    avatar: '../../images/photo_img.png',
    logo_class: '',
    teamName: '',
    date: '',
    nowdate: '',
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
      if (e.target.dataset.current == "0") {//建立的球队


      } else {//加入的球队
        wx.reLaunch({
          url: '/pages/team/team',
        })
      }
      
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this ;
    var _DATE = utils.formatDate(new Date());
    that.setData({
      session_key: wx.getStorageSync("token"),
      nowdate: _DATE
    })
  },
  //建立球队
  creationTeam:function(){
    wx.navigateTo({
      url: '/pages/createTeam/createTeam',
    })
  },

  // 上传logo
  changeAvatar: function () {

    var that = this;
    // var childId = wx.getStorageSync("child_id");
    var token = wx.getStorageSync('token');

    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        var avatar = res.tempFilePaths;
        wx.uploadFile({
          url: app.globalData.httpsPath + '/upload',
          method: "POST",
          filePath: avatar[0],
          name: 'pic',
          header: {
            "content-type": "multipart/form-data"
          },
          formData: {
            //和服务器约定的token, 一般也可以放在header中
            'token': token
          },
          success: function (res) {
            var _imgdata = JSON.parse(res.data);
            if (res.statusCode != 200) {
              wx.showModal({
                title: '提示',
                content: '上传失败',
                showCancel: false
              })
              return;
            }
            var _imgpath = _imgdata.data
            that.setData({  //上传成功修改显示头像
              avatar: app.globalData.imagePath + _imgpath
            })
          },
          fail: function (e) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
          },
          complete: function () {
            wx.hideToast();  //隐藏Toast
          }
        });
      }
    })
  },
  // 球队名称
  teamnameInput: function (event) {
    this.setData({
      teamName: event.detail.value
    })
  },
  //日期
  bindDateChange: function (event) {
    this.setData({
      date: event.detail.value
    })
  },
  //所在地区
  areaInput: function (event) {
    this.setData({
      address: event.detail.value
    })
  },
  //保存数据
  creationTeam: function () {
    var that = this;

    if (that.data.avatar == '../../images/photo_img.png') {
      this.setData({
        logo_class: "error-create-text"
      });
      return false;
    }
    if (that.data.teamName == '') {
      this.setData({
        form_teamname_class: "form-control-error"
      });
      return false;
    }
    if (that.data.date == '') {
      this.setData({
        form_date_class: "form-control-error"
      });
      return false;
    }
    var _token = wx.getStorageSync("token");

    var _imgpath = that.data.avatar.split("cn/");
    wx.request({
      url: app.globalData.httpsPath + '/team/save', // 接口地址
      method: "POST",
      header: {
        'content-type': 'application/json', // 默认值
        // 'token': _token
      },
      data: {
        token: _token,
        name: that.data.teamName,
        logo: _imgpath[1],
        since_year: that.data.date
      },
      success(res) {
        console.info(res.data)
        wx.showToast({
          title: '创建成功',
          icon: 'succes',
          duration: 3000,
          mask: true
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '../personalCenter/personalCenter'
          })
        }, 3000)

      }
    })
  },

 
  
})